﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;


namespace TCOChart
{
    public partial class FormMESUI_AntUI : Form
    {
        private UserControl currControl;
        private bool isUpdatingTabs = false;//用于阻止Tabs更新

        public FormMESUI_AntUI()
        {
            InitializeComponent();
            this.Width = 1340;
            this.Height = 770;
            //this.WindowState = FormWindowState.Maximized;

            loadMainTabs();
        }

        /// <summary>
        /// 加载mes窗体
        /// </summary>
        private void loadMainTabs()
        {
            {
                FormEmbedder formEmbedder = new FormEmbedder
                {
                    FormType = typeof(FormMesOpen),
                    Dock = DockStyle.Fill,
                };
                AntdUI.TabPage tabPage = new AntdUI.TabPage()
                {
                    Text = "测试",
                    ReadOnly = true,
                    IconSvg = "BgColorsOutlined",
                };
                tabPage.Controls.Add(formEmbedder);
                tabs.Pages.Add(tabPage);
            }

            {
                FormEmbedder formEmbedder = new FormEmbedder
                {
                    FormType = typeof(MesFormEngrave),
                    Dock = DockStyle.Fill,
                    //Size = new System.Drawing.Size()
                };
                AntdUI.TabPage tabPage = new AntdUI.TabPage()
                {
                    Text = "刻码",
                    ReadOnly = true,
                    IconSvg = "BgColorsOutlined"
                };
                tabPage.Controls.Add(formEmbedder);
                tabs.Pages.Add(tabPage);
            }

            {
                var form = new MesFormEngrave();
                form.TopLevel = false;
                form.Dock = DockStyle.Fill;
                form.FormBorderStyle = FormBorderStyle.None;
                //form.Size = new Size(1300, 600);

                form.Show();
                panel2.Controls.Add(form);
            }
        }


        private void Menu1_SelectChanged(object sender, AntdUI.MenuSelectEventArgs e)
        {
            string name = (string)e.Value.Text;

            foreach (var tab in tabs.Pages)
            {
                if (tab is AntdUI.TabPage existingTab && existingTab.Text == name)
                {
                    isUpdatingTabs = true;
                    tabs.SelectedTab = existingTab; // 直接跳转到已存在的 TabPage
                    isUpdatingTabs = false;
                    currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
                    return;
                }
            }

            UserControl control = null;
            switch (name)
            {
                case "item1":
                    control = new MenuForm();
                    break;
                case "固定":
                    control = new FormPie();
                    break;
                default:
                    break;
            }

            if (control != null)
            {
                control.Dock = DockStyle.Fill;
                AntdUI.TabPage tabPage = new AntdUI.TabPage()
                {
                    Text = name,
                };
                tabPage.Controls.Add(control);
                tabs.Pages.Add(tabPage);
                isUpdatingTabs = true;
                tabs.SelectedTab = tabPage;
                isUpdatingTabs = false;
                currControl = control;
            }
        }

        private void button_collapse_Click(object sender, EventArgs e)
        {
            button_collapse.Toggle = !button_collapse.Toggle;
            tabs.TabMenuVisible = !tabs.TabMenuVisible;
        }

        private void Button_home_Click(object sender, EventArgs e)
        {
            UserControl control = new FormPort();
            AddMenuTab("主页", control);
        }
        private void Button_data_Click(object sender, EventArgs e)
        {
            UserControl control = new FormPie();
            AddMenuTab("数据", control);
        }

        /// <summary>
        /// 用于添加新建tab页面
        /// </summary>
        /// <param name="name"></param>
        /// <param name="control"></param>
        void AddMenuTab(string name, UserControl control)
        {
            foreach (var tab in tabs.Pages)
            {
                if (tab is AntdUI.TabPage existingTab && existingTab.Text == name)
                {
                    isUpdatingTabs = true;
                    tabs.SelectedTab = existingTab; // 直接跳转到已存在的 TabPage
                    isUpdatingTabs = false;
                    currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
                    return;
                }
            }

            if (control != null)
            {
                control.Dock = DockStyle.Fill;
                AntdUI.TabPage tabPage = new AntdUI.TabPage()
                {
                    Text = name,
                };
                
                if (tabs.TabMenuVisible)
                {
                    tabPage.Controls.Add(control);
                    tabs.Pages.Add(tabPage);
                    isUpdatingTabs = true;
                    tabs.SelectedTab = tabPage;
                    isUpdatingTabs = false;
                    currControl = control;
                }
            }
        }

        private void segmented1_ItemClick(object sender, AntdUI.SegmentedItemEventArgs e)
        {
            string name = e.Item.Text.ToString();
            UserControl control = null;
            if (name == "主页")
            {
                //control = new FormPort();
                control = new FormPieFailure();
            }
            else if(name == "数据")
            {
                control = new FormPie();
            }
            else if(name == "报警")
            {
                control = new FormPieTest();
            }

            AddMenuTab(name, control);

            segmented3.SelectIndex = -1;
            segmented2.SelectIndex = -1;
        }

        private void segmented2_ItemClick(object sender, AntdUI.SegmentedItemEventArgs e)
        {

            segmented1.SelectIndex = -1;
            segmented3.SelectIndex = -1;
        }

        private void segmented3_ItemClick(object sender, AntdUI.SegmentedItemEventArgs e)
        {

            segmented1.SelectIndex = -1;
            segmented2.SelectIndex = -1;
        }
    }




    /// <summary>
    /// 负责将任何 Windows Form 转换为可嵌入的控件
    /// </summary>
    public class FormEmbedder : UserControl
    {
        public Type FormType { get; set; }
        private Form EmbeddedForm { get; set; }
        public FormEmbedder()
        {
            this.Resize += (s, e) => ResizeEmbeddedForm();
        }
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            if (FormType != null && DesignMode == false)
            {
                LoadForm();
            }
        }
        private void LoadForm()
        {
            // 创建窗体实例
            EmbeddedForm = Activator.CreateInstance(FormType) as Form;
            EmbeddedForm.TopLevel = false;
            EmbeddedForm.FormBorderStyle = FormBorderStyle.None;
            EmbeddedForm.Dock = DockStyle.Fill;
            EmbeddedForm.Visible = true;
            EmbeddedForm.Size = this.Size;

            // 添加到当前控件
            this.Controls.Add(EmbeddedForm);
        }
        // 当容器大小变化时调整嵌入式窗体
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            if (EmbeddedForm != null)
            {
                EmbeddedForm.Size = this.Size;
            }
        }
        private void ResizeEmbeddedForm()
        {
            if (EmbeddedForm != null && !EmbeddedForm.IsDisposed)
            {
                EmbeddedForm.Size = this.Size;
            }
        }
    }
}
